<template>
  <div>
    <UploadExcel :on-success="successFn" />
  </div>
</template>

<script>
import { userbatchAPI } from '@/api/employees'
export default {
  methods: {
    async successFn({ header, results }) {
      const userObj = {
        '手机号': 'mobile',
        '姓名': 'username',
        '工号': 'workNumber',
        '入职日期': 'timeOfEntry',
        '转正日期': 'correctionTime'
      }
      const arr = results.map(item => {
        const users = {}
        Object.keys(item).forEach(key => {
          if (userObj[key] === 'timeOfEntry' || userObj[key] === 'correctionTime') {
            users[userObj[key]] = this.formatDate(item[key])
          } else {
            users[userObj[key]] = item[key]
          }
        })
        return users
      })
      await userbatchAPI(arr)
      this.$router.push('/employees')
    },
    formatDate(numb, format) { // 转化exeal日期格式
      const time = new Date((numb - 1) * 24 * 3600000 + 1)
      time.setYear(time.getFullYear() - 70)
      const year = time.getFullYear() + ''
      const month = time.getMonth() + 1 + ''
      const date = time.getDate() - 1 + ''
      if (format && format.length === 1) {
        return year + format + month + format + date
      }
      return year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date)
    }
  }
}
</script>

<style>
</style>
